{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "plt.rcParams['font.sans-serif']=['SimHei']\n",
    "plt.rcParams['axes.unicode_minus']=False\n",
    "\n",
    "data=pd.read_csv(r'.\\datas\\air_tianjin_2017.csv',names=['Date','Quality_level','AQI','AQI_Rank','PM'])\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X=data[:30].Date\n",
    "Y=data[:30].PM     #选取数据的前30行，画图\n",
    "plt.plot(X,Y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data['month']=data.Date.map(lambda x: x[5:7])   #map和apply都可以实现这个功能\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "del(data['month2'])\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mean_aqi=data.groupby('month')['AQI'].mean()\n",
    "mean_aqi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X=np.arange(1,13)\n",
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Y=np.array(mean_aqi)\n",
    "Y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_ticks=['%d月'%i for i in X]\n",
    "plt.xticks(X,x_ticks)\n",
    "plt.plot(X,Y,label='AQI per month')\n",
    "plt.legend()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "aqi_season1=data.query('month==[\"01\",\"02\",\"03\"]').AQI.mean()\n",
    "aqi_season1=round(aqi_season1,2)\n",
    "aqi_season1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "aqi_season2=data.query('month==[\"04\",\"05\",\"06\"]').AQI.mean()\n",
    "aqi_season2=round(aqi_season2,2)\n",
    "aqi_season2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "aqi_season3=data.query('month==[\"07\",\"08\",\"09\"]').AQI.mean()\n",
    "aqi_season3=round(aqi_season3,2)\n",
    "aqi_season3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "aqi_season4=data.query('month==[\"10\",\"11\",\"12\"]').AQI.mean()\n",
    "aqi_season4=round(aqi_season4,2)\n",
    "aqi_season4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "labels=['第%d季度'%i for i in [1,2,3,4]]\n",
    "labels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "aqis=[aqi_season1,aqi_season2,aqi_season3,aqi_season4]\n",
    "aqis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.pie(aqis,labels=labels,autopct='%1.2f%%',shadow=True,explode=[0,0.7,0.2,0.1])\n",
    "#第一个参数是要用来画图的数据，label是每一块的标签，autopct是饼上的显示内容，shadow定义是否有阴影\n",
    "#explode定义那一块浮出\n",
    "plt.axis('equal')  #让饼图正起来"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
